package com.tianji.learning.mq;

import com.tianji.common.constants.MqConstants;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mq.message.SignMessage;
import com.tianji.learning.service.IPointsRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import static com.tianji.common.constants.MqConstants.Exchange.LEARNING_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.WRITE_REPLY;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2024/6/29 15:18
 */
@Component
@RequiredArgsConstructor
public class LearningPointsRecordListener {
    private final IPointsRecordService pointsRecordService;

    /**
     * 1、评论回复积分消费者（Day7 - 课堂）
     * @param userId
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "qa.points.queue",durable = "true"),
            exchange = @Exchange(name = LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = WRITE_REPLY
    ))
    public void listenWriteReplyMessage(Long userId){
        pointsRecordService.addPointsRecord(userId, 5, PointsRecordType.QA);
    }

    /**
     * 2、签到积分消费者（Day7 - 课堂）
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "sign.points.queue",durable = "true"),
            exchange = @Exchange(name = LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.SIGN_IN
    ))
    public void listenSignMessage(SignMessage message){
        pointsRecordService.addPointsRecord(message.getUserId(), message.getPoints(), PointsRecordType.SIGN);
    }
}
